<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Commander;
use App\Admin\Server\DropdownMenu;
use App\Admin\Server\UserInfoServer;
use App\Models\CommanderAudit;
use App\Models\UserInfo;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class CommanderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Commander(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('uid');
            $grid->column('username');
            $grid->column('tel');
            $grid->column('card_id');
            $grid->column('site');
            $grid->column('district');
            $grid->column('status','是否审核')->switch();
            $grid->column('detailed_address');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Commander(), function (Show $show) {
            $show->field('id');
            $show->field('uid');
            $show->field('username');
            $show->field('tel');
            $show->field('card_id');
            $show->field('site');
            $show->field('district');
            $show->field('detailed_address');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Commander(), function (Form $form) {

            $form->selectTable('id','用户id')->rules('required')->from(UserInfoServer::make())->saving(function ($e){
                return $e[0];
            });


            $form->text('uid');
            //用户姓名
            $form->text('username');
            //用户手机号
            $form->text('tel');
            //身份证号
            $form->text('card_id');
            //自提点信息
            $form->text('site');
            //小区地址
            $form->text('district');
            //详细地址
            $form->text('detailed_address');

            $form->display('created_at');
            $form->display('updated_at');
            $form->saved(function (Form $form){
                if($form->isEditing()){
                    $status = $this->status;
                    if($status==1){
                        return ;
                    }

                    DB::beginTransaction();
                    try {
                        $data = UserInfo::where('id',$this->uid)->first();
                        CommanderAudit::create([
                            'uid'=>$this->uid,
                            'username'=>$this->username,
                            'tel'=>$this->tel,
                            'what_city'=>$this->site,
                            'distribution_price'=>1.00,
                            'audit_name'=>$data->nickName,
                            'pick_site'=>$this->site,
                            'created_at'=>$this->created_at,
                            'audit_time'=>$this->updated_at,
                            'audit_state'=>$this->status
                        ]);

                        UserInfo::where('id',$this->uid)->update(['commander_id'=>1]);
                        \App\Models\Commander::where('id',$this->id)->delete();
                        DB::commit();
                    }catch (\Exception $exception){
                        return '报错';
                        DB::rollBack();
                    }
                }
            });
        });
    }
}
